<?php
if (!current_user_can('configure_gengo')) {
	die( __('You do not have sufficient permissions to access this page.') );
}

if ($_POST['gengo_synonyms_submit'] && !empty($_POST['update_synonyms'])) {
	foreach ($_POST['update_synonyms'] as $category_id) {
		foreach ($gengo->languages as $language_id => $entry) {
			$synonym = $_POST[$category_id . '_' . $language_id];
			$sanitised = sanitize_title($synonym);
			$wpdb->query("UPDATE $gengo->cat2syn_table SET synonym = '$synonym', sanitised = '$sanitised' WHERE cat_id = $category_id AND language_id = $language_id");
		}
	}
	$gengo->update_message(__("Category translations updated.", GENGO_DOMAIN));
}
elseif ($_POST['gengo_synblock_submit']) {
	if (-1 == $_POST['gengo_synblock_name']) {
		if (!$block_name = $_POST['gengo_new_synblock']) {
			$gengo->error_message(__("Please specify a name for the new snippet.", GENGO_DOMAIN));
			$synblock_failed = true;
		} elseif ($block_name == $wpdb->get_var("SELECT block_name FROM $gengo->synblock_table WHERE block_name = '$block_name' LIMIT 1")) {
			$gengo->error_message(sprintf(__("Snippet '%s' already exists, please choose another name.", GENGO_DOMAIN), $block_name));
			$synblock_failed = true;
		} else {
			foreach ($gengo->languages as $language_id => $entry) $values[] = "('$block_name', $language_id, '" . str_replace("\n", '<br />', $_POST['synblock_' . $language_id]) . "')";
			if ($values) $wpdb->query("INSERT INTO $gengo->synblock_table(block_name, language_id, text) VALUES " . implode(', ', $values));
			$gengo->update_message(sprintf(__("Snippet '%s' created.", GENGO_DOMAIN), $block_name));
		}
	} else {
		$block_name = $_POST['gengo_synblock_name'];
		foreach ($gengo->languages as $language_id => $entry) {
			$text = str_replace("\n", '<br />', $_POST['synblock_' . $language_id]);
			$wpdb->query("UPDATE $gengo->synblock_table SET text = '$text' WHERE block_name = '$block_name' AND language_id = $language_id");
		}
		$gengo->update_message(sprintf(__("Snippet '%s' updated.", GENGO_DOMAIN), $block_name));
	}
}
elseif ($_POST['gengo_delete_synblock']) {
  $block_name = $_POST['gengo_synblock_name'];
	if (!$affected = $wpdb->query("DELETE FROM $gengo->synblock_table WHERE block_name = '$block_name'")) $gengo->error_message(sprintf(__("Synonym block '%s' does not exist.", GENGO_DOMAIN), $block_name));
	else $gengo->update_message(sprintf(__("Snippet '%s' deleted.", GENGO_DOMAIN), $block_name));
}
?>
<div class="wrap">
<h2><?php _e('Category Translations', GENGO_DOMAIN); ?></h2>
<p><?php _e("Add translations for category names here to translate your categories and category permalinks.  The underlying category name will be used for display and permalink purposes if there is no synonym specified for a language.  Editing the underlying category does not alter these translations.", GENGO_DOMAIN); ?></p>
<?php
if ($results = $wpdb->get_results("SELECT cat_id, language_id, synonym FROM $gengo->cat2syn_table ORDER BY cat_id, language_id")) {
	?>
	<form id="gengo_synonyms_form" method="post">
	<table id="gengo_synonyms_table" cellpadding="3" cellspacing="3">
	<tr>
		<th scope="col">ID</th>
		<th scope="col"></th>
		<?php
		foreach ($gengo->languages as $entry) {
			?>
			<th scope="col"><?php echo $entry->language ?></th>
			<?php
		}
		?>
	</tr>
	<?php
	foreach ($results as $result) {
		if ($previous_id != $result->cat_id) {
			// Starting a new row.
			if ($previous_id) { ?></tr><?php }
			?>
			<tr class="alternate">
			<th scope="row"><?php echo $result->cat_id ?></th>
			<td align="right"><input type="checkbox" name="update_synonyms[]" id="check_<?php echo $result->cat_id ?>" value="<?php echo $result->cat_id ?>" /></td>
			<?php
		}
		?>
		<td><input type="text" name="<?php echo $result->cat_id . "_" . $result->language_id ?>" value="<?php echo $result->synonym ?>" onkeydown="gengo_set_synonym_checkbox(<?php echo $result->cat_id ?>)" /></td>
		<?php
		$previous_id = $result->cat_id;
	}
	?>
	</tr>
	</table>
	<p class="submit">
	<input type="submit" id="gengo_synonyms_submit" name="gengo_synonyms_submit" value="<?php _e('Update Checked', GENGO_DOMAIN) ?>" />
	</p>
	</form>
	</div>
	<div class="wrap"><p><strong><?php _e('Note:', GENGO_DOMAIN) ?></strong> <?php _e('These are only translations.  Changing these values will <strong>not</strong> alter the underlying category.', GENGO_DOMAIN) ?></p>
	<?php
} else {
	?>
	<p><?php _e('No languages defined yet.', GENGO_DOMAIN) ?></p>
	<?php
}
?>
</div>
<?php
if ($count = count($gengo->languages)) {
	$block_names = $wpdb->get_col("SELECT DISTINCT block_name FROM $gengo->synblock_table");
	$block_list = '<option value="-1">' . __('(Add New Snippet)', GENGO_DOMAIN) . '</option>';
	foreach ($block_names as $block_name) $block_list .= "<option value=\"$block_name\">$block_name</option>";
?>
<script type="text/javascript">
var add_button_text = '<?php _e('Add Synonym Block', GENGO_DOMAIN) ?>';
var update_button_text = '<?php _e('Update Synonym Block', GENGO_DOMAIN) ?>';
</script>
<div class="wrap">
<h2><?php _e('Snippets', GENGO_DOMAIN); ?></h2>
<p><?php _e("Snippets are small blocks of translated text for use throughout your site.  You can display these blocks anywhere by using gengo_snippet('snippet_name') in your template.", GENGO_DOMAIN); ?></p>
<form id="gengo_synblocks_form" method="post">
<p style="float: left"><label for="gengo_synblock_name"><?php _e('Snippet Name:', GENGO_DOMAIN) ?></label><br />
<select id="gengo_synblock_name" name="gengo_synblock_name" onchange="gengo_get_synblock(this.value);"><?php echo $block_list ?></select></p>
<p style="float: left; margin-left: 50px" id="gengo_new_synblock_block"><label for="gengo_new_synblock"><?php _e('New Snippet Name:', GENGO_DOMAIN) ?></label><br />
<input type="text" id="gengo_new_synblock" name="gengo_new_synblock" /></p>
<?php
foreach ($gengo->languages as $language_id => $entry) {
	?>
	<p style="clear: both"><label for="synblock_<?php echo $language_id ?>"><?php echo $entry->language ?>:<br />
	<textarea id="synblock_<?php echo $language_id ?>" name="synblock_<?php echo $language_id ?>" style="width: 75%"><?php if ($synblock_failed) echo $_POST['synblock_' . $language_id]; ?></textarea></p>
	<?php
}
?>
<p class="submit">
<input style="float: left; display: none;" type="submit" id="deletepost" name="gengo_delete_synblock" value="<?php _e('Delete Snippet', GENGO_DOMAIN) ?>" onclick="return confirm('<?php _e("You are about to delete this snippet.\\n\'Cancel\' to stop, \'OK\' to delete", GENGO_DOMAIN) ?>')" />
<input type="submit" name="gengo_synblock_submit" id="gengo_synblock_submit" value="<?php _e('Add Snippet', GENGO_DOMAIN) ?>" />
</p>
</form>
<?php
} else {
	?>
	<p><?php _e('No languages defined yet.', GENGO_DOMAIN) ?></p>
	<?php
}
?>
</div>